package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

public class MainActivity extends AppCompatActivity {
    private MVIew mvIew;
    private ScrollView scrollView;
    private LinearLayout main_layout;
    private ImageView IV_image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化自定义的View
      /*  MVIew mvIew=new MVIew(getApplicationContext());
        ScrollView main_layout=findViewById(R.id.sll_scrollView);
        main_layout.addView(mvIew);*/
        initDate();
        //getwidth();

    }


    ///显示View的截图

    private void initDate() {
        main_layout=findViewById(R.id.main_layout);
        scrollView=findViewById(R.id.scrollview);
        IV_image=findViewById(R.id.IV_image);
    }
    //获取宽高
    private void getwidth() {
        // 获取ScrollView 实际高度
        int h = 0;
        for (int i = 0; i < scrollView.getChildCount(); i++) {
            h += scrollView.getChildAt(i).getHeight();
            scrollView.getChildAt(i).setBackgroundResource(android.R.color.white);
        }
        // 创建对应大小的bitmap
        Bitmap bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,     Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        scrollView.draw(canvas);


    }
    /**
     * 获取一个 View 的缓存视图
     *  (前提是这个View已经渲染完成显示在页面上)
     * @param view
     * @return
     */
    public static Bitmap getCacheBitmapFromView(View view) {
        final boolean drawingCacheEnabled = true;
        view.setDrawingCacheEnabled(drawingCacheEnabled);
        view.buildDrawingCache(drawingCacheEnabled);
        final Bitmap drawingCache = view.getDrawingCache();
        Bitmap bitmap;
        if (drawingCache != null) {
            bitmap = Bitmap.createBitmap(drawingCache);
            view.setDrawingCacheEnabled(false);
        } else {
            bitmap = null;
        }
        return bitmap;
    }
    //点击生成图片
    public void bt_toimage(View view) {
       /* Bitmap bitmap = getCacheBitmapFromView(scrollView);
        IV_image.setImageBitmap(bitmap);*/
       Bitmap bitmap=SimpleUtils.shotScrollView(scrollView);
       IV_image.setImageBitmap(bitmap);
    }

    // 获取指定Activity的截屏，保存到png文件
    public static Bitmap takeScreenShot(Activity activity) {
        // View是你需要截图的View
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();

        // 获取状态栏高度
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
//        L.e(""+statusBarHeight);

        // 获取屏幕长和高
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();
        // 去掉标题栏
        // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
        int hh = statusBarHeight+50;
        Log.e("截取的高---", String.valueOf(statusBarHeight));
        Log.e("屏幕的高--", String.valueOf(height));
        Bitmap b = Bitmap.createBitmap(b1, 0, hh, width, height -hh);
        view.destroyDrawingCache();
        Log.e("Thresh", "takeScreenShot:            截图成功");
        return b;
    }

}


























